package com.neusoft.databus.policy.service.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.common.bean.MessageBean;
import com.neusoft.bizcore.common.messaging.ProducerService;
import com.neusoft.bizcore.web.utils.JsonUtils;
import com.neusoft.databus.common.bean.CommandBean;
import com.neusoft.databus.common.bean.SnmptrapBean;
import com.neusoft.databus.policy.config.DatabusPolicyProperties;
import com.neusoft.databus.policy.constants.DatabusPolicyConstant;
import com.neusoft.databus.policy.enums.EventStatus;
import com.neusoft.databus.policy.enums.PolicyType;
import com.neusoft.databus.policy.expression.ExpressionHandler;
import com.neusoft.databus.policy.service.MessageServie;
import com.neusoft.databus.policy.utils.EventCountUtil;
import com.neusoft.databus.policy.vo.EventVO;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class MessageServieImpl implements MessageServie {
    @Autowired
    private ExpressionHandler policyExpressionHandler;
    @Autowired
    private ExpressionHandler complexPolicyExpressionHandler;
    @Autowired
    private DatabusPolicyProperties policyProperties;
    @Autowired
    private ProducerService producerService;
    @Value("${databus.policy.eventTopic}")
    private String topic;
    @Autowired
    private EventCountUtil eventCountUtil;

    @Override
    public void process(final CommandBean commandBean) {
        // 确保触发策略检查的数据都是顺序的。
        this.policyExpressionHandler.execute(commandBean);
        //复杂告警策略
        if (this.policyProperties.getComplexPolicy()) {
            this.complexPolicyExpressionHandler.execute(commandBean);
        }
    }

    @Override
    public void processTrapMessage(final SnmptrapBean snmptrapBean) {
        final EventVO eventVO = new EventVO();
        eventVO.setId(snmptrapBean.getIdentifier());
        eventVO.setType(PolicyType.SUB_SYSTEM_ALARM);
        if (StringUtils.isBlank(snmptrapBean.getMetricVariable())) {
            MessageServieImpl.log.warn("第三方子系统告警未获取到指标变量名");
        }
        eventVO.setMetricCode(snmptrapBean.getMetricVariable());
        if (StringUtils.isBlank(snmptrapBean.getMetricName())) {
            MessageServieImpl.log.warn("第三方子系统告警未获取到指标名称");
        }
        eventVO.setMetricName(snmptrapBean.getMetricName());
        if (StringUtils.isBlank(snmptrapBean.getResourceId())) {
            MessageServieImpl.log.warn("第三方子系统告警未获取到资源ID");
        }
        eventVO.setResourceCode(snmptrapBean.getResourceId());
        if (StringUtils.isBlank(snmptrapBean.getResourceName())) {
            MessageServieImpl.log.warn("第三方子系统告警未获取到资源名称");
        }
        eventVO.setResourceName(snmptrapBean.getResourceName());
        if (StringUtils.isBlank(snmptrapBean.getAlarmGrade())) {
            MessageServieImpl.log.warn("第三方子系统告警未获取到告警等级");
        }
        eventVO.setPriority(snmptrapBean.getAlarmGrade());
        if (StringUtils.isBlank(snmptrapBean.getAlarmType())) {
            MessageServieImpl.log.warn("第三方子系统告警未获取到告警类型");
        }
        eventVO.setAlarmType(snmptrapBean.getAlarmType());
        if (StringUtils.isBlank(snmptrapBean.getContent())) {
            MessageServieImpl.log.warn("第三方子系统告警未获取到告警内容");
        }
        eventVO.setContent(snmptrapBean.getContent());
        if (StringUtils.isBlank(snmptrapBean.getSubSystemNo())) {
            MessageServieImpl.log.warn("第三方子系统告警未获取到系统编码");
        }
        eventVO.setSubSystemCode(snmptrapBean.getSubSystemNo());
        if(StringUtils.isBlank(snmptrapBean.getConfirmer())) {
        	MessageServieImpl.log.warn("第三方子系统告警未获取到告警确认人");
        }
        eventVO.setConfirmer(StringUtils.isBlank(snmptrapBean.getConfirmer()) ? "" : snmptrapBean.getConfirmer());
        if (DatabusPolicyConstant.SNMP_TRAP_UNFIXED.equals(snmptrapBean.getMessageType())) {
            eventVO.setStatus(EventStatus.UNFIXED);
            eventVO.setLastTime(snmptrapBean.getReportTime());
        } else if (DatabusPolicyConstant.SNMP_TRAP_FIXED.equals(snmptrapBean.getMessageType())) {
            eventVO.setStatus(EventStatus.FIXED);
            eventVO.setFixTime(snmptrapBean.getReportTime());
        } else if (DatabusPolicyConstant.SNMP_TRAP_CONFIRM.equals(snmptrapBean.getMessageType())){
        	eventVO.setStatus(EventStatus.CONFIRM);
            eventVO.setLastTime(snmptrapBean.getReportTime());
        } else {
            MessageServieImpl.log.warn("第三方子系统告警未获取到消息类型");
        }
        this.sendMessage(eventVO);
    }

    private void sendMessage(final EventVO eventVO) {
        final MessageBean message = new MessageBean(JsonUtils.pojoToJson(eventVO), new Date());
        this.producerService.send(this.topic, message);
        /**
         * 告警计数
         */
        this.eventCountUtil.eventCount(eventVO.getType(), eventVO.getStatus());
    }
}
